<?php
class Form_Table
{
	protected $_table;
	protected $_form;
	protected $_groupe;
	public function getForm($table)
	{
		$this->_table = new Wmp_Db_Table_Generic();
		$this->_table->setTable($table);		
		
		$this->_groupe = array();
		$formMetadata = $this->_table->info('metadata');
		
		$this->_form = new Zend_Dojo_Form();
		$this->_form->setAction('/user/login')//TODO:
		     ->setMethod('post');//TODO:

		/*     
		$this->_form->setDecorators(array(
            'FormElements',
            array('AccordionContainer', array(
                'id' => 'tabContainer',
                'style' => 'width: 600px; height: 2000px;',
                
            )),
            'DijitForm',
        ));
		*/
        $i=0;
        foreach($formMetadata as $col => $params) {
			$this->_addFormElement($params);
			$i++;
			
			//if($i>=8)
				//break;
			break;     	
        }

        $height=($i*30)+400;
		
        $this->_form->setDecorators(array(
            'FormElements',
            array('AccordionContainer', array(
                'id' => 'tabContainer',
                'style' => 'width: 600px; height: '.$height.'px;',
                /*'dijitParams' => array(
                    'tabPosition' => 'top'
                ),*/ //TODO:
            )),
            'DijitForm',
        ));
        
		//TODO
		/*
		// Create and configure username element:
		$username = $this->_form->createElement('text', 'username');
		$username->addValidator('alnum')
		         ->addValidator('regex', false, array('/^[a-z]+/'))
		         ->addValidator('stringLength', false, array(6, 20))
		         ->setRequired(true)
		         ->addFilter('StringToLower');
		
		// Create and configure password element:
		$password = $this->_form->createElement('password', 'password');
		$password->addValidator('StringLength', false, array(6))
		         ->setRequired(true);
		
		// Add elements to form:
		$this->_form->addElement($username)
		     ->addElement($password)
		     // use addElement() as a factory to create 'Login' button:
		     ->addElement('submit', 'login', array('label' => 'Login'));
		*/
		
		return $this->_form;
		//exit(1);
	}
	
	protected function _addFormElement($params)
	{
		 $name		= "";
	     $lenght	= 0;
	     $type		= "";
	     $default	= "";
	     $primary	= "";
	     $legend	= "";
	     foreach($params as $key => $val) {
	     	switch($key) {
	     		case 'COLUMN_NAME':
	     			$name		= $val;
	     			break;
	     			
	     		case 'DATA_TYPE':
	     			$type		= $val; 
	     			/*if(in_array($type,array("text","varchar","tinytext","char","meddiumtext","longtext"))) {
	     				$type="text";
	     			}
	     			else if(in_array($type, array("int","tinyint","mediumint","smallint","bigint"))) {
	     				$validator = new Zend_Validate_Int();
	     			}
	     			else if(in_array($type,array("date","datetime","timestamp","time","year"))) {
	     				
	     			}
	     			else if($type=="") {
	     				
	     			}*/
	     			break;
	     			
	     		case 'LENGTH':
	     			$lenght		= $val;
	     			break;
	     			
	     		case 'DEFAULT':
	     			$default	= $val; 
	     			break;
	     			
	     		case 'PRIMARY':
	     			$primary	= $val;
	     			break;				
	     	}
	     }	     
		$legend = $name.' - (type : '.$type.', lenght : '.$lenght.', default : '.$default.')';
	    if($primary) $legend .=' PRIMARY';

	   $this->_groupe[$name] = new Zend_Dojo_Form_SubForm();
	   $this->_groupe[$name]->setDecorators(array(
		    'FormElements',
		    array('HtmlTag', array('tag' => 'dl')),
		    'AccordionPane',
		));
	   $this->_groupe[$name]->setAttribs(array(
            'name'   => $name.time(),
            'legend' => 'Text Elements',
            'dijitParams' => array(
                'title' => $legend,
            ),
        ));
        
        $tabForm = new Zend_Dojo_Form_SubForm();
	    $tabForm->setDecorators(array(
		    'FormElements',
		    array('HtmlTag', array('tag' => 'dl')),
		    'TabContainer',
		    'dijitParams' => array(
                    'tabPosition' => 'top'
                ),
		));
		$tabForm->setAttribs(array(
            'name'   => 'tab'.time(),
            
        ));	

		$labelForm = new Zend_Dojo_Form_SubForm();
        $labelForm->setAttribs(array(
            'name'   => 'labelfdfd',
            'legend' => 'Label',
            'dijitParams' => array(
                'title' => 'Labellm',
            ),
        ));	    
	    $col = new Zend_Form_Element_Text('col');
	    $col->setLabel('Label');
	    $col->setValue($name);	        
	    $labelForm->addElement($col);
	    return $labelForm;
	    Zend_Debug::dump($labelForm);exit(1); 
	    $this->_groupe[$name]->addSubForm($labelForm);
	    
	    /*$types = new Zend_Form_Element_Select('types');
        $types->setLabel('Type');
        $types->setRequired(true);
        //$types->addValidator(new Zend_Validate_Int());        		
		$types->addMultiOptions( array(	'Button'=>'Button',
										'Captcha'=>'Captcha',
										'Checkbox'=>'Checkbox',
										'File'=>'File',
										'Hidden'=>'Hidden',
										'Hash'=>'Hash',
										'Image'=>'Image',
										'MultiCheckbox'=>'MultiCheckbox',
										'Multiselect'=>'Multiselect',
										'Password'=>'Password',
										'Radio'=>'Radio',
										'Reset'=>'Reset',
										'Select'=>'Select',
										'Submit'=>'Submit',
										'Text'=>'Text',
										'Textarea'=>'Textarea'
										));
		$this->_groupe[$name]->addElement($types);
										
		$Filtres = new Zend_Form_Element_MultiCheckbox('filtre');
        $Filtres->setLabel('Filtres');
        $Filtres->setRequired(true);
        $Filtres->addValidator(new Zend_Validate_Int());        		
		$Filtres->addMultiOptions( array(	'Alnum'=>'Alphanumeriq',
											'Alpha'=>'Alphabetic ',
											'BaseName'=>'BaseName',
											'Digits'=>'Digits',
											'Dir'=>'Dir',
											'HtmlEntities'=>'HtmlEntities',
											'Int'=>'Int',
											'StripNewlines'=>'StripNewlines',
											'RealPath'=>'RealPath',
											'StringToLower'=>'StringToLower',
											'StringToUpper'=>'StringToUpper',
											'StringTrim'=>'StringTrim',
											'StripTags'=>'StripTags'											
									));
		
		$this->_groupe[$name]->addElement($Filtres); 
		
		//$this->_groupe[$name]1 = $this->_form->addDisplayGroup(array('col','types','filtre'), 'DB proprities', array('legend'=>$legend));
	    //$this->_groupe[$name]1->setLegend('legende');
		*/
	    $this->_form->addSubForm($this->_groupe[$name], time().$name);
	    
	    /*$element = new Zend_Form_Element_Select('parentforumid');
        $element->setLabel($name);
        $element->setRequired(true);
        $element->addValidator($validator);
        $element->addMultiOption(null, 'root category');
	    
	    
		
	    $element->addValidator('alnum')
		         ->addValidator('regex', false, array('/^[a-z]+/'))
		         ->addValidator('stringLength', false, array(6, 20))
		         ->setRequired(true)
		         ->addFilter('StringToLower');
		$this->_form->addDisplayGroup(array('username', 'password'), 'DB proprities');
		         
		echo $name.', '.$type.', '.$lenght.', '.$default.', '.$primary.'<br>';
		*/
	    
	   //return $this->_form;
	    
	   //exit(1);
	}
	
	protected function _getFileters()
	{
		
	}
	
	protected function _getValidators()
	{
		
	}
	
	protected function _getDecorators()
	{
		
	}
	
	public function setFilters()
	{
		
	}
	
	public function setValidators()
	{
		
	}
	
	public function setDecorators()
	{
		
	}
	
	public function setParams()
	{
		
	}
}